/**
 * 
 */
package br.com.cnpv.modelo.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import br.com.cnpv.modelo.acesso.idao.IClienteDAO;
import br.com.cnpv.modelo.entity.Cliente;

/**
 * @author ricardo
 *
 */
public class ClienteDAO implements IClienteDAO {

	
	@SuppressWarnings("unchecked")
	@Override
	public List<Cliente> buscar(Cliente cliente, Session session) throws HibernateException {
		
		String clausula = "";
		Query query = null;
		
		if(cliente.getCdPess() != null && StringUtils.isNotEmpty(cliente.getCdPess())){
		
			clausula = cliente.getCdPess();
			
			query = session.getNamedQuery("Cliente.buscarByCdPess");

		}else{
			
			clausula = "%" + cliente.getNome() + "%";
			query = session.getNamedQuery("Cliente.buscarByNome");
		}
		
		query.setString("clausula", clausula);
		
		return query.list();
	}

	public Cliente saveOrUpdate(Cliente cliente, Session session)
			throws HibernateException {
		
		return (Cliente) session.merge(cliente);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Cliente> listar(Session session) throws HibernateException 
	{
		Query query = session.getNamedQuery("Cliente.listar");
		return query.list();
	}
	
	
}
